package eee.xiang.cloud_learning.service.design.responsibility;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author Eee_xiang
 * @Date 2021/10/28
 **/
@Data
public class FilterChain {

    private List<Filter> filters = new ArrayList<Filter>();
    private int index;

    public FilterChain() {
        index = 0;
    }

    public FilterChain addFilter(Filter f) {
        filters.add(f);
        return this;
    }

    public void doFilter(Request request, Reponse response, FilterChain chain) {
        if (index == filters.size()) {
            return;
        }
        //获取当前过滤器
        Filter f = filters.get(index);
        //下一次获取的时候，就是下一个过滤器了
        index++;
        //执行当前过滤器的过滤方法
        f.doFilter(request, response, chain);
    }
}
